package com.aliwo.recursion;

/**
 * @author xuyayuan
 * @description 阶乘问题
 * @date 2021/9/7 10:31
 */
public class FactorialRecursive {

    /**
     * 功能描述:阶乘的递归实现
     * @author xuyayuan
     * @param n
     * @return long
     */
    public static long f(int n) {
        if (n==1) {
            System.out.println("递归前 n:" + n);
            return 1;
        } else {
            System.out.println("递归前 n :" + n);
            long result = n * f(n - 1);
            System.out.println("递归后 n:" + n + "   " + "result:" + result);
            return result;
        }
    }

    /**
     * 功能描述:非递归的阶乘实现
     * @author xuyayuan
     * @param n
     * @return long
     */
    public static long f_loop(int n) {
       long result = n;
        while (n >1) {
            n--;
            result = result * n;
        }
        return result;
    }

    public static void main(String[] args) {
        //System.out.println("f(5):"+ f(5));

        System.out.println("f_loop(5):"+ f_loop(5));
    }
}
